package com.paul.sys.controller.back;

import com.paul.sys.service.DigroadInfoService;

import com.paul.sys.tools.PageData;
import com.paul.sys.util.ParamUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.paul.sys.model.DigroadInfo;

/**
 * Created by 羽 on 2015/12/17.
 */
@Controller
@RequestMapping("/back/digroad/")
public class DigroadController {
    private static Logger logger = Logger.getLogger(DigroadController.class);

    @Autowired
    private DigroadInfoService digroadInfoService;

    @RequestMapping("ismenu/digroad")
    public String constructInfo() throws Exception {

        return "back/digroad/digroad-list";
    }

    @RequestMapping("ismenu/digroadmap")
    public String constructMapInfo() throws Exception {

        return "back/digroad/digroad-map";
    }

    @ResponseBody
    @RequestMapping("listAll")
    public Map<String, Object> listAll() {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<DigroadInfo> data = digroadInfoService.listNoEnd();
            result.put("data", data);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }


    @ResponseBody
    @RequestMapping("list")
    public Map<String, Object> listdigroad(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();

        try {
            ParamUtil util = new ParamUtil(request);
            Map<String, String> param = util.getParamMap();
            PageData<DigroadInfo> digs = digroadInfoService.listByPage(param);
            result.put("totalRows", digs.getPageBean().getTotalNum());
            result.put("curPage", param.get("curPage"));
            result.put("data", digs.getListData());
            result.put("success", true);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }

        return result;
    }


    @ResponseBody
    @RequestMapping("edit")
    public Map<String, Object> setDiginfo(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            ParamUtil util = new ParamUtil(request);
            Map<String, String> param = util.getParamMap();
            JSONObject jsonObject = JSONObject.fromObject(param);
            DigroadInfo digroadInfo = (DigroadInfo) JSONObject.toBean(jsonObject, DigroadInfo.class);

            //表示新增
          if(param.get("add")!=null){
              SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
              String date = format.format(new Date());
              if(digroadInfo.getConstructstarttime().compareTo(date)<0){
                  digroadInfo.setStatus("0");
              }else{
                  digroadInfo.setStatus("3");
              }
              digroadInfoService.insertSelective(digroadInfo);

          }else {//表示修改
              digroadInfoService.updateByRecord(digroadInfo);
          }
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }


    @ResponseBody
    @RequestMapping("delete")
    public String deletedig(HttpServletRequest request){
        String ids  = request.getParameter("id").toString();
        String[] idArr = ids.split(",");
        String result = "true";
        try{
            digroadInfoService.deleteByIds(idArr);
        }catch(Exception e){
            result = "false";
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return result;
    }



    @RequestMapping("edit_page")
    public ModelAndView doModify(HttpServletRequest request) {
        ModelAndView view = null;
        if (request.getParameter("id") != null) {
            view = new ModelAndView("back/digroad/digroad-edit");
            DigroadInfo entity = digroadInfoService.selectByPrimaryKey(request.getParameter("id").toString());
            view.addObject("digInfo", entity);
            view.addObject("type", "edit");
        } else {
            view = new ModelAndView("back/digroad/digroad-add");

        }
        return view;
    }

    @ResponseBody
    @RequestMapping("updateStatus")
    public String updateStatus(HttpServletRequest request){
        String result = "";
        try {
            DigroadInfo entity = new DigroadInfo();
            entity.setId(new Integer(request.getParameter("id").toString()));
            entity.setStatus("1");
            digroadInfoService.updateByRecord(entity);
            result = "true";
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
}
}
